package com.li.sort;

import java.util.Arrays;

public class RadixSort {
    public static void main(String[] args) {
        int[] arr = {53, 3, 542, 748, 14, 214};
        radixsort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void radixsort(int[] arr) {
        int[][] bucket = new int[10][arr.length];
        //记录每个桶实际存放了多少个数据
        int[] bucketElementCounts = new int[10];
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        //�õ�������Ǽ�λ��
        int maxLength = (max + "").length();

        for (int a = 0, n = 1; a < maxLength; a++, n *= 10) {
            for (int j = 0; j < arr.length; j++) {
                int digit = arr[j] / n % 10;//个位的值
                bucket[digit][bucketElementCounts[digit]] = arr[j];
                bucketElementCounts[digit]++;
            }
            int index = 0;
            for (int i = 0; i < 10; i++) {
                if (bucketElementCounts[i] != 0) {
                    for (int l = 0; l < bucketElementCounts[i]; l++) {
                        arr[index++] = bucket[i][l];
                    }
                }
                bucketElementCounts[i] = 0;

            }

        }
        /*for (int j=0;j<arr.length;j++){
            int digit=arr[j]%10;//个位的值
            bucket[digit][bucketElementCounts[digit]]=arr[j];
            bucketElementCounts[digit]++;
        }
        int index=0;
        for (int i=0;i<10;i++){
            if (bucketElementCounts[i]!=0){
                for (int l=0;l<bucketElementCounts[i];l++){
                    arr[index++]=bucket[i][l];
                }
            }
            bucketElementCounts[i]=0;

        }
        for (int j=0;j<arr.length;j++){
            int digit=arr[j]/10%10;//个位的值
            bucket[digit][bucketElementCounts[digit]]=arr[j];
            bucketElementCounts[digit]++;
        }
        index=0;
        for (int i=0;i<10;i++){
            if (bucketElementCounts[i]!=0){
                for (int l=0;l<bucketElementCounts[i];l++){
                    arr[index++]=bucket[i][l];
                }
            }
            bucketElementCounts[i]=0;
        }
        for (int j=0;j<arr.length;j++){
            int digit=arr[j]/100%10;//个位的值
            bucket[digit][bucketElementCounts[digit]]=arr[j];
            bucketElementCounts[digit]++;
        }
        index=0;
        for (int i=0;i<10;i++){
            if (bucketElementCounts[i]!=0){
                for (int l=0;l<bucketElementCounts[i];l++){
                    arr[index++]=bucket[i][l];
                }
            }
            bucketElementCounts[i]=0;
        }
*/
    }
}
